package jman.competition12;

import java.sql.SQLException;
import java.util.HashMap;

public class Manager {
	//已经登录的用户，用于登录以后的操作
	private HashMap<String,User> loginUser=new HashMap<String,User>();
	//注册
	public boolean registered(String name,String password){
		boolean result=false;
		User user=new User(name,password);
		DatebaseManager dm=new DatebaseManager();
		dm.getCon();
		dm.read("select * from user where username='"+user.getUserName()+"'");
		try {
			if(dm.getRs().next()){
				System.out.println("该用户名已被注册");
			}else{
				dm.add("insert into user (username,password) values ('"+user.getUserName()+"','"+user.getPassword()+"')");
				dm.add("insert into capitalaccount (username) values ('"+user.getUserName()+"')");
				System.out.println("注册成功");
				result=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("注册失败");
		}
		dm.closeRs();
		dm.closePs();
		dm.closeCon();
		return result;
	}
	//登录
	public boolean login(String name,String password){
		boolean result=false;
		User user=new User(name,password);
		DatebaseManager dm=new DatebaseManager();
		dm.getCon();
		dm.read("select * from user where username='"+user.getUserName()+"'");
		try {
			if(dm.getRs().next()){
				if(dm.getRs().getString("password").equals(user.getPassword())){
					result=true;
					System.out.println("登录成功");
					loginUser.put(user.getUserName(), user);
				}else{
					System.out.println("登录失败");
				}
			}else{
				System.out.println("登录失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("登录失败");
		}
		dm.closeRs();
		dm.closePs();
		dm.closeCon();
		return result;
	}
	//退出登录
	public boolean signout(String userName){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			result=true;
			loginUser.remove(userName);
		}
		return result;
	}
	public boolean updateCapitalaccount(String userName){
		boolean result=false;
		DatebaseManager dm=new DatebaseManager();
		dm.getCon();
		dm.update("update capitalaccount set money="+loginUser.get(userName).getC().getMoney()+" where username='"+userName+"'");
		result=true;
		dm.closePs();
		dm.closeCon();
		return result;
	}
	public boolean add(String userName,double money){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			loginUser.get(userName).add(money);
			updateCapitalaccount(userName);
		}
		return result;
	}
	public boolean transfer(String userName,double money,User user){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			DatebaseManager dm=new DatebaseManager();
			dm.getCon();
			dm.read("select * from capitalaccount where username='"+user.getUserName()+"'");
			try {
				if(dm.getRs().next()){
					if(loginUser.get(userName).getC().getMoney()>=money){
						loginUser.get(userName).transfer(money, user);
						result=true;
					}
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dm.closeRs();
			dm.closePs();
			dm.closeCon();
			updateCapitalaccount(userName);
			dm.getCon();
			dm.update("update capitalaccount set money="+user.getC().getMoney()+" where username='"+user.getUserName()+"'");
			dm.closePs();
			dm.closeCon();
		}
		return result;
	}
	public boolean logout(String userName){
		boolean result=false;
		if(loginUser.containsKey(userName)){
			DatebaseManager dm=new DatebaseManager();
			dm.getCon();
			loginUser.get(userName).logout();
			dm.delete("delete from capitalaccount where username='"+userName+"'");
			result=true;
			dm.closePs();
			dm.closeCon();
		}
		return result;
	}
}
